//
//  BaseTabBarViewController.m
//  CarUser
//
//  Created by 王森林 on 2018/11/29.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "HomeViewController.h"
#import "ChooseViewController.h"
#import "CarcircleViewController.h"
#import "NewsViewController.h"
#import "MeViewController.h"
#import "BaseNavViewController.h"
#import "BaseTabBar.h"

#import "LoginViewController.h"

@interface BaseTabBarViewController ()<UITabBarControllerDelegate,MSTabBarViewDelegate>
@property  (nonatomic, strong) BaseTabBar *baseTabBar;
@end

@implementation BaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(logInvcAlert) name:NotificationCenterName_AlertLoginVC object:nil];
    
    
    
    [self initTabBar];
    
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{
                                                        NSForegroundColorAttributeName:Common_TextColor,
                                                        NSFontAttributeName:[UIFont fontWithName:NORMAL_FONT_NAME size:12],
                                                        NSBackgroundColorAttributeName:UIColor.whiteColor
                                                        } forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:Common_TextLowColor,
                                                        NSFontAttributeName:[UIFont fontWithName:NORMAL_FONT_NAME size:12],
                                                        NSBackgroundColorAttributeName:UIColor.whiteColor
                                                        } forState:UIControlStateNormal];
//    [UITabBar appearance].translucent = NO;
//    [[UITabBar appearance] setBackgroundImage:[UIImage new]];
//    [[UITabBar appearance] setShadowImage:[UIImage new]];
//
//    [[UITabBar appearance] setBackgroundImage:IMG(@"tab_bg")];
//    [[UITabBar appearance] setBackgroundColor:[UIColor redColor]];
    
    
    // 初始化所有的子控制器
    [self setupAllChildViewControllers];
    // Do any additional setup after loading the view.
}


- (void) initTabBar
{
    
//    _baseTabBar = [[BaseTabBar alloc] init];
//    [_baseTabBar.centerBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    //选中时的颜色
//    _baseTabBar.tintColor = [UIColor colorWithRed:27.0/255.0 green:118.0/255.0 blue:208/255.0 alpha:1];
//    //透明设置为NO，显示白色，view的高度到tabbar顶部截止，YES的话到底部
//    _baseTabBar.translucent = NO;
//    //利用KVC 将自己的tabbar赋给系统tabBar
//    [self setValue:_baseTabBar forKeyPath:@"tabBar"];
//
//
//    self.delegate = self;
    
    BaseTabBar *tabBar = [[BaseTabBar alloc] init];
    tabBar.tabBarView.viewDelegate = self;
    [self setValue:tabBar forKey:@"tabBar"];
    

}


#pragma mark - MSCustomTabBarViewDelegate

- (void)msTabBarView:(MSTabBarView *)view didSelectItemAtIndex:(NSInteger)index
{
    // 切换到对应index的viewController
    NSLog(@"index%ld",index);
    self.selectedIndex = index;
}

- (void)msTabBarViewDidClickCenterItem:(MSTabBarView *)view
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"点击了中间的按钮" message:@"do something!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertController addAction:action];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)buttonAction:(UIButton *)button{
    self.selectedIndex = 2;//关联中间按钮
//    [self rotationAnimation];
}

/**
 *  初始化所有的子控制器
 */
- (void)setupAllChildViewControllers{
    
    HomeViewController *homeViewController = [[HomeViewController alloc] init];
    UIViewController *home = [self setupChildViewController:homeViewController title:@"首页" imageName:@"tab_home" selectedImageName:@"tab_home"];
    
    
    ChooseViewController *chooseViewController = [[ChooseViewController alloc] init];
    UIViewController *choose = [self setupChildViewController:chooseViewController title:@"选车" imageName:@"tab_home" selectedImageName:@"tab_choose"];
    
    CarcircleViewController *carcircleViewController = [[CarcircleViewController alloc] init];
    UIViewController *carcircle = [self setupChildViewController:carcircleViewController title:@"车友圈" imageName:@"" selectedImageName:@""];
    
    NewsViewController *newsViewController = [[NewsViewController alloc] init ];
    UIViewController *news = [self setupChildViewController:newsViewController title:@"服务" imageName:@"tab_home" selectedImageName:@"tab_news"];
    
    MeViewController *meViewController = [[MeViewController alloc] init];
    UIViewController *me = [self setupChildViewController:meViewController title:@"我的 " imageName:@"tab_home" selectedImageName:@"tab_me"];
    
    [self setViewControllers:@[home,choose,carcircle,news,me]];

}

- (UIViewController *)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    // 1.设置控制器的属性
    childVc.title = title;
//    childVc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    BaseNavViewController *navi = [[BaseNavViewController alloc]initWithRootViewController:childVc];
    return navi;
}


/**
 只是跳出登录
 */
- (void)logInvcAlert{
    [BaseTabBarViewController alertLoginVC];
}

+ (void)alertLoginVC{
    if ([GetAppRootViewController.parentViewController isKindOfClass:NSClassFromString(@"LoginViewController")]) {
        return;
    }
    BaseNavViewController *loginNavi =  [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginNavi"];
    dispatch_async(dispatch_get_main_queue(), ^{
        [GetAppRootViewController presentViewController:loginNavi animated:YES completion:nil];
    });
}



//UITabBarDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 2){//选中中间的按钮
//        [self rotationAnimation];
    }else {
//        [_mcTabbar.centerBtn.layer removeAllAnimations];
    }
}








/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
